<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>平方练习</title>
</head>
<body>
<input type="text">
<input type="button" value="平方" onclick="f()">
<h3>
    结果：<span></span>
</h3>

<script>
    let i = document.querySelector("input");
    let s = document.querySelector("span");

    function f() {
        //判断输入的内容是否是数值
        if (isNaN(i.value)) {
            s.innerText = "输入错误";
            return;
        }
        //字符串在进行-*/运算时会自动转成数值类型
        s.innerText = i.value * i.value;

        //单独做加法是字符串拼接
        //s.innerText = i.value + i.value;

        //把字符串转成数值后再进行加法运算
        //s.innerText = parseFloat(i.value)+parseFloat(i.value); //方法1
        s.innerText = i.value * 1 + i.value * 1; //方法2
    }
</script>

</body>
</html>